<template>
  <div style="height:100%;width:100%;">
    <div id="barLineCanvas" style="height:100%;width:100%;" ref="barLineCanvas"></div>
  </div>
</template>

<script>
export default {
  name: "barLineCanvas",
  data() {
    return {};
  },
  props: {
    barData: {
      type: Array,
    },
    id: {
      type: Number,
    },
  },
  watch: {
    barData(val) {
 
      this.init(val);
    },
  },
  mounted() {
    // this.init();
  },
  methods: {
    init(val) {
      let xdata = [];
      if (this.id == 1) {
        xdata = val.map((i) => i.jsdwAh);
      } else {
        xdata = val.map((i) => i.yjzyK);
      }

      let obj = {
        本年转固金额: {
          type: "bar",
          data: val.map((i) => i.bnzge),
        },
        转固率: {
          type: "line",
          data: val.map((i) => {
            if (i.zgPer) {
              return i.zgPer;
            } else {
              return 0;
            }
          }),
        },
      };
      let legendata = Object.keys(obj);

      var myChart = this.$echarts.init(this.$refs.barLineCanvas);
      let option = {
        color: [
          //自定义的颜色
          new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "#49DFFE" },
            { offset: 1, color: "#41A2FD" }, //柱图渐变色
          ]),
          "#F3707C",
        ],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },

        grid: {
          left: "1%",
          right: "1%",
          bottom: "5%",
          top: "20%",
          containLabel: true,
        },
        legend: {
          data: legendata,
          right: "3%",
          top: "4%",
          // itemWidth: 12,
          itemHeight: 12,
          itemGap: 20,
          textStyle: {
            fontSize: 12,
          },
        },
        xAxis: [
          {
            type: "category",
            data: xdata,
            axisPointer: {
              type: "shadow",
            },
            axisLabel: {
              show: true,
              rotate: 30,
              // textStyle: {
              //   color: "#000",
              //   fontSize: 12,
              // },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            // axisLine: {
            //   lineStyle: {
            //     color: "#eee",
            //     width: 1, // 粗细
            //   },
            // },
          },
        ],
        yAxis: [
          {
            // type: this.id == '1' ? "log" : 'value',
            type: 'value',
            // interval: 50,
            name: "单位：万元",
            nameTextStyle: {
              fontSize: 14,
              padding: [0, 0, 5, 50],
              color: "rgba(0, 0, 0, 0.4)",
            },
            axisLine: {
              lineStyle: {
                color: "#eee",
                width: 1, // 粗细
              },
            },

            axisLabel: {
              show: true,
              // formatter: "{value} ml",
              textStyle: {
                color: "#000",
                fontSize: 12,
              },
            },
            axisTick: {
              show: false,
            },
          },
          {
            type: "value",

            splitLine: {
              //网格线
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "#eee",
                width: 1, // 粗细
              },
            },
            axisLabel: {
              show: true,
              formatter: "{value} %",
              textStyle: {
                color: "#000",
                fontSize: 12,
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: legendata[0],
            type: "bar",
            label: {
              show: true,
              position: "top",
              //  rotate: 30
            },
            barMaxWidth: "18px",

            // barGap: 0,
            data: obj[legendata[0]].data,
          },

          {
            name: legendata[1],
            type: "line",
            yAxisIndex: 1,
            smooth: false,
            lineStyle: {
              color: "#F3707C",
              width: 3,
            },
            data: obj[legendata[1]].data,
          },
        ],
      };
      myChart.setOption(option);
      setTimeout(function () {
        window.addEventListener("resize", () => {
          myChart.resize();
        });
      }, 500);
    },
  },
};
</script>

<style>
</style>
